from django.shortcuts import render

# Create your views here.
from .areasSerializer import AreaSerializers, AreasSerializers
from .models import Areas
from rest_framework.generics import ListAPIView, RetrieveAPIView

from rest_framework_extensions.cache.mixins import CacheResponseMixin
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.permissions import IsAuthenticated


# class AreasView(ListAPIView):
#     """
#     省份列表
#     """
#     serializer_class = areasSerializer.AreasSerializers
#     queryset = Areas.objects.filter(parent=None)
#
#
# class AreaView(RetrieveAPIView):
#     """
#     获取当前行政区划信息以及子行政区划信息
#     """
#     queryset = Areas.objects.all()
#     serializer_class = areasSerializer.AreaSerializers



# 设置缓存类，一定要放在最左边
class AreaViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    """
    地址视图
    """
    # 设置分页属性为none
    pagination_class = None

    # 更具pk指来判断获取不同的序列化器

    def get_serializer_class(self):

        "当前请求没有pk值就是list，查询所有省份"
        # 更具不同的请求选择不同的序列化器
        if self.action == "list":

            return AreasSerializers

        else:
            return AreaSerializers


            # 根据不同请求选不同的选择不同的查询集

    def get_queryset(self):

        if self.action == "list":
            return Areas.objects.filter(parent=None)

        else:
            return Areas.objects.all()
